<?php

namespace App\Http\Controllers;

use App\Http\Consts\LangConsts;
use App\Http\Consts\MapConsts;
use App\Http\Requests\ListRequest;
use App\Models\Article;
use App\Models\Game;
use App\Models\Map;
use App\Models\System;
use Illuminate\Support\Facades\Cookie;

class VisitorConsultController
{
    public function tk(){
        return c_return(0,[
            'tk' => c_set_cookie(request()->ip())
        ]);
    }

    public function gameListV2(){
        $map = new Map();
        $selectIds = $map
            ->select(['id','name'])
            ->where('type',MapConsts::MAP_TYPE_GAME)
            ->where('lang_type',LangConsts::getLangCodeByStr(get_lang()))
            ->where('status',1)
            ->get();

        $gameModel = new Game();
        $data = [];
        foreach($selectIds as $k => $v){
            $query = $gameModel->select([
                'id',
                'name',
                'game_type_id',
                'game_type_id as game_type_str',
                'show_img_id',
                'synopsis',
                'id as key',
            ])
            ->where('status',1)
            ->where('game_type_id',$v['id'])
            ->where('enable_status',1)
            ->orderBy('id','desc')
            ->take(10)
            ->get();
            if(!empty($query->toArray())){
                $data[] = [
                    'type_name' => $v['name'],
                    'type_id' => $v['id'],
                    'list' => $query,
                ];
            }
        }
        return c_return(0,$data);
    }

    public function gameListV1(ListRequest $request){
        $pageSize = $request->post('page_size',1);
        $gameModel = new Game();
        $filters = $request['filters'];
        $query = $gameModel->select([
            'id',
            'name',
            'game_type_id',
            'game_type_id as game_type_str',
            'show_img_id',
            'synopsis',
            'id as key',
        ])->where('status',1)
            ->where('enable_status',1);
        if(!empty($filters)){
            if(!empty($filters['game_type_id'])){
                $query = $query->where('game_type_id',$filters['game_type_id']);
            }
            if(!empty($filters['name'])){
                $query = $query->where('name','like','%'.$filters['name'].'%');
            }
        }

        $data = $query->paginate($pageSize);
        if(isset($filters['game_type_id'])){
            $map = new Map();
            return response()->json([
                'code' => 0,
                'data' => get_simple_page($data),
                'type_name' =>$map->where('id',$filters['game_type_id'])->first()['name'],
                'type_id' => $filters['game_type_id']
            ]);
        }
        return c_return(0,get_simple_page($data));
    }

    public function gameListHot(){
        $systemModel = new System();

        $gameModel = new Game();
        $data = $systemModel->select(['games_carousel_image'])->where('id',1)->first();
        if(!empty($data)){
            $fmt = json_decode($data['games_carousel_image'],true);
            $resultData = [];
            foreach($fmt as $key => $value){
                $d = $gameModel->select([
                    'id',
                    'name',
                    'game_type_id',
                    'game_type_id as game_type_str',
                    'show_img_id',
                    'synopsis',
                    'id as key',
                ])
                    ->where('id',$value['game_id'])
                    ->where('status',1)
                    ->where('enable_status',1)
                    ->first();

                if(!empty($d)){
                    $resultData[$key] = $d;
                    if($value['img_id']){
                        $resultData[$key]['show_img_id'] = $value['img_id'];
                    }

                }
            }

            if(!empty($resultData)){
                return c_return(0,$resultData);
            }

        }
        $data = $gameModel->select([
            'id',
            'name',
            'game_type_id',
            'game_type_id as game_type_str',
            'show_img_id',
            'synopsis',
            'id as key',
        ])->where('status',1)
            ->where('enable_status',1)
            ->orderBy('id','desc')
            ->take(10)
            ->get();

        return c_return(0,$data);
    }

    public function articleListTop(){
        $articleModel = new Article();
        $data = $articleModel->select([
            'id',
            'title',
            'article_type_id',
            'article_type_id as article_type_str',
            'synopsis',
            'show_img_id',
            'created_at',
            'id as key',
        ])->where('status',1)
        ->where('is_top',1)
        ->where('enable_status',1)
        ->orderBy('id','desc')
        ->take(1)
        ->get();
        if(empty($data->toArray())){
            $data = $articleModel->select([
                'id',
                'title',
                'article_type_id',
                'article_type_id as article_type_str',
                'synopsis',
                'show_img_id',
                'created_at',
                'id as key',
            ])->where('status',1)
                ->where('enable_status',1)
                ->orderBy('id','desc')
                ->take(1)
                ->get();
        }


        return c_return(0,$data);
    }

    public function articleListHot(){
        $articleModel = new Article();
        $data = $articleModel->select([
            'id',
            'title',
            'article_type_id',
            'article_type_id as article_type_str',
            'synopsis',
            'show_img_id',
            'created_at',
            'id as key',
        ])->where('status',1)
            ->where('is_hot',1)
            ->where('enable_status',1)
            ->orderBy('id','desc')
            ->take(3)
            ->get();
        if(empty($data->toArray())){
            $data = $articleModel->select([
                'id',
                'title',
                'article_type_id',
                'article_type_id as article_type_str',
                'synopsis',
                'show_img_id',
                'created_at',
                'id as key',
            ])->where('status',1)
                ->where('enable_status',1)
                ->orderBy('id','desc')
                ->take(3)
                ->get();
        }
        return c_return(0,$data);
    }

    public function articlePA(){
        $articleModel = new Article();
        $data = $articleModel->select([
            'id',
            'title',
            'article_type_id',
            'article_type_id as article_type_str',
            'synopsis',
            'show_img_id',
            'created_at',
            'id as key',
        ])->where('status',1)
            ->inRandomOrder()
            ->where('enable_status',1)
            ->orderBy('id','desc')
            ->take(10)
            ->get();
        return c_return(0,$data);
    }

    public function articleListV1(ListRequest $request)
    {
        $pageSize = $request->post('page_size', 1);
        $articleModel = new Article();

        $filters = $request['filters'];
        $query = $articleModel->select([
            'id',
            'title',
            'article_type_id',
            'article_type_id as article_type_str',
            'synopsis',
            'show_img_id',
            'created_at',
            'id as key',
        ])->where('status', 1)
            ->where('enable_status', 1)
            ->orderBy('id', 'desc');
        if (!empty($filters)) {
            if (!empty($filters['article_type_id'])) {
                $query = $query->where('article_type_id', $filters['article_type_id']);
            }
            if (!empty($filters['title'])) {
                $query = $query->where('title','like', '%'.$filters['title'].'%');
            }
        }

        $data = $query->paginate($pageSize);
        if(isset($filters['article_type_id'])){
            $map = new Map();
            return response()->json([
                'code' => 0,
                'data' => get_simple_page($data),
                'type_name' =>$map->where('id',$filters['article_type_id'])->first()['name'],
                'type_id' => $filters['article_type_id']
            ]);
        }
        return c_return(0, get_simple_page($data));
    }

    public function articleListV2(){
        $map = new Map();
        $selectIds = $map
            ->select(['id','name'])
            ->where('type',MapConsts::MAP_TYPE_ARTICLE)
            ->where('lang_type',LangConsts::getLangCodeByStr(get_lang()))
            ->where('status',1)
            ->get();

        $data = [];
        $articleModel = new Article();
        foreach($selectIds as $k => $v){
            $query = $articleModel->select([
                'id',
                'title',
                'article_type_id as article_type_str',
                'synopsis',
                'show_img_id',
                'created_at',
                'id as key',
            ])->where('status',1)
                ->where('article_type_id',$v['id'])
                ->where('enable_status',1)
                ->orderBy('id','desc')
                ->take(3)
                ->get();
            if(!empty($query->toArray())){
                $data[] = [
                    'type_name' => $v['name'],
                    'type_id' => $v['id'],
                    'list' => $query,
                ];
            }
        }
        return c_return(0,$data);
    }

    public function gameInfo(){
        $id = request()->post('id',0);
        $gameModel = new Game();
        $data = $gameModel
            ->select([
                'id',
                'id as key',
                'name',
                'synopsis',
                'content',
                'show_img_id',
                'carousel_ids as carousel_ids_arr',
                'game_type_id',
                'game_type_id as game_type_str',
                'os',
                'version',
                'size',
                'update',
                'a_download',
                'ios_download',
            ])
            ->where('id',$id)
            ->where('status',1)
            ->where('enable_status',1)
            ->orderBy('id','desc')
            ->first();
        if($data){
            // recommend
            $recommend = $gameModel->select([
                'id',
                'name',
                'show_img_id',
                'id as key',
            ])->where('status',1)
                ->where('id','!=',$id)
                ->where('enable_status',1)
                ->where('game_type_id',$data['game_type_id'])
                ->take(9)
                ->get();
            $data['recommend_list'] = $recommend;
        }
        return c_return(0,$data);

    }

    public function articleInfo(){
        $id = request()->post('id',0);
        $articleModel = new Article();
        $data = $articleModel
            ->select([
                'id',
                'id as key',
                'title',
                'synopsis',
                'author',
                'content',
                'show_img_id',
                'article_type_id',
                'article_type_id as article_type_str',
                'created_at'
            ])
            ->where('status',1)
            ->where('enable_status',1)
            ->where('id',$id)
            ->first();

        if($data){
            // 获取前一个ID
            $previous = $articleModel
                ->select([
                    'id',
                    'title'
                ])
                ->where('status', 1)
                ->where('enable_status', 1)
                ->where('id', '<', $id)
                ->orderBy('id', 'desc')
                ->first();

            // 获取下一个ID
            $next = $articleModel
                ->select([
                    'id',
                    'title'
                ])
                ->where('status', 1)
                ->where('enable_status', 1)
                ->where('id', '>', $id)
                ->orderBy('id', 'asc')
                ->first('id');

            $data['previous'] = $previous;
            $data['next'] = $next;
        }
        Cookie::queue(1, 2, 3);
//        c_set_cookie(request()->ip());
        return c_return(0,$data);
    }
}
